package utils

import (
	"os"
	"strconv"
	"sync"

	//"github.com/astaxie/beego"

	"github.com/go-redis/redis"
)

var client *redis.Client
var once sync.Once

//var mu sync.Mutex
func initRedis() {

	once.Do(func() {
		host := os.Getenv("redis_host")
		passWord := os.Getenv("redis_password")
		db := os.Getenv("redis_db")
		// //string到int
		dbNum, _ := strconv.Atoi(db)
		poolsize := os.Getenv("redis_poolsize")
		//string到int
		poolsizeNum, _ := strconv.Atoi(poolsize)
		client = redis.NewClient(&redis.Options{
			Addr:     host,
			Password: passWord, // no password set
			DB:       dbNum,    // use default DB
			PoolSize: poolsizeNum,
		})

		_, err := client.Ping().Result()
		if err != nil {
			panic(err)
		}
	})
}

func GetClient() redis.Client {
	initRedis()

	return *client
}
